package com.wkx.controller;/**
 * @className: RoleController
 * @description: TODO 类描述
 * @author: wkx
 * @date: 2022/4/11
 **/

import com.wkx.pojo.Menu;
import com.wkx.pojo.Roles;
import com.wkx.service.MenuService;
import com.wkx.service.RoleService;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : RoleController  //类名

 * @Author : Lenovo //作者
 * @Date: 2022/4/11  11:03
 */
@Controller
@RequestMapping("role")
public class RoleController {
    private Logger logger = LoggerFactory.getLogger(AdminController.class);
    @Reference(url = "dubbo://localhost:20881")
    private MenuService menuService;
    @Reference(url = "dubbo://localhost:20881")
    private RoleService roleService;

    @RequestMapping("toRole")
    public String toRole(Model model, @RequestParam(value = "page",defaultValue = "1") Integer page){
        logger.info("跳转至角色管理页面");

        List<Roles> rolesList = roleService.getRolesList();


        model.addAttribute("rolesList",rolesList);

        return "role";
    }

    @RequestMapping("voke")
    public String voke(Integer rid,Model model){
        logger.info("跳转至权限授权页面");

        Map<Integer,Object> map = new HashMap<>();
        List<Menu> roleMenuList= menuService.getRoleMenuListByRid(rid);
        List<Menu> fMenuList = menuService.getMenuList(0, null);
        map.put(0,fMenuList);
        for (Menu m : fMenuList) {
            List<Menu> sMenuList = menuService.getMenuList(m.getId(), null);
            map.put(m.getId(),sMenuList);
        }
        StringBuilder s = new StringBuilder();
        for (Menu menu: roleMenuList) {
            s.append(menu.getId());
        }
        Roles role = roleService.getRoleByRid(rid);

        model.addAttribute("s", s.toString());
        model.addAttribute("map",map);
        model.addAttribute("role",role);

        return "voke";
    }

    @RequestMapping("toVoke")
    public String toVoke(Integer[] roles,Integer rid){
        logger.info("角色授权后跳转至用户角色管理页面");
        if (roles==null) {
            roles = new Integer[0];
        }
        menuService.updateRoleMenus(roles,rid);
        return "redirect:/role/toRole";
    }

    @RequestMapping("addRole")
    public String addRole(Roles roles){
        logger.info("添加角色");
        roleService.addRole(roles);

        return "redirect:/role/toRole";
    }
    @RequestMapping("toAddRole")
    public String toAddRole(){
        logger.info("跳转到添加角色页面");


        return "addRole";
    }
}
